package com.ruoyi.school.event;

import com.ruoyi.school.SchoolService;
import org.openapi.common.ApiException;
import org.openapi.event.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 新增学生请假信息前，查询出学期、当前用户所属院系、专业班级等
 */
@Component
public class InsertLeaveEvent implements EventService {
    @Autowired
    private SchoolService schoolService;

    @Override
    public void process(Object data) {
        Map semester = schoolService.getSemester();
        if(semester == null){
            throw new ApiException("学期已结束");
        }
        Map userInfo = schoolService.getUserInfo();
        if(userInfo == null || !"student".equalsIgnoreCase(userInfo.get("_identity").toString())){
            throw new ApiException("您不是学生");
        }

        Map<String,Object> leaveData = (Map<String,Object>)data;
        leaveData.put("sem_id", semester.get("id"));
        leaveData.put("dept_id", userInfo.get("dept_id"));
        leaveData.put("major_id", userInfo.get("major_id"));
        leaveData.put("grade_id", userInfo.get("grade_id"));
        leaveData.put("class_id", userInfo.get("class_id"));
        leaveData.put("student_id", userInfo.get("id"));
    }
}
